package com.learning.platform.repository;

import com.learning.platform.entity.Exam;
import com.learning.platform.enums.ExamStatus;
import com.learning.platform.enums.ExamType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface ExamRepository extends JpaRepository<Exam, Long> {
    
    /**
     * 分页查询课程考试
     */
    @Query("SELECT e FROM Exam e WHERE e.courseId = :courseId " +
           "AND (:examType IS NULL OR e.examType = :examType) " +
           "AND (:status IS NULL OR e.status = :status) " +
           "ORDER BY e.createdAt DESC")
    Page<Exam> findByCourseId(@Param("courseId") Long courseId,
                            @Param("examType") ExamType examType,
                            @Param("status") ExamStatus status,
                            Pageable pageable);
    
    /**
     * 查询课程下的所有已发布考试
     */
    List<Exam> findByCourseIdAndStatusOrderByCreatedAtDesc(Long courseId, ExamStatus status);
    
    /**
     * 分页查询课程下指定状态的考试
     */
    Page<Exam> findByCourseIdAndStatusOrderByCreatedAtDesc(Long courseId, ExamStatus status, Pageable pageable);
    
    /**
     * 查询教师创建的考试
     */
    Page<Exam> findByCreatedByOrderByCreatedAtDesc(Long createdBy, Pageable pageable);
    
    /**
     * 查询当前可参加的考试（已发布且在时间范围内）
     */
    @Query("SELECT e FROM Exam e WHERE e.courseId = :courseId " +
           "AND e.status = 'PUBLISHED' " +
           "AND (e.startTime IS NULL OR e.startTime <= :now) " +
           "AND (e.endTime IS NULL OR e.endTime >= :now) " +
           "ORDER BY e.createdAt DESC")
    List<Exam> findAvailableExams(@Param("courseId") Long courseId, @Param("now") LocalDateTime now);
    
    /**
     * 统计课程考试数量
     */
    long countByCourseId(Long courseId);
    
    /**
     * 按考试类型统计
     */
    @Query("SELECT e.examType, COUNT(e) FROM Exam e WHERE e.courseId = :courseId GROUP BY e.examType")
    List<Object[]> countByCourseIdGroupByType(@Param("courseId") Long courseId);
    
    /**
     * 按状态统计
     */
    @Query("SELECT e.status, COUNT(e) FROM Exam e WHERE e.courseId = :courseId GROUP BY e.status")
    List<Object[]> countByCourseIdGroupByStatus(@Param("courseId") Long courseId);
    
    /**
     * 搜索考试
     */
    @Query("SELECT e FROM Exam e WHERE " +
           "(:courseId IS NULL OR e.courseId = :courseId) " +
           "AND (:examType IS NULL OR CAST(e.examType AS string) = :examType) " +
           "AND (:status IS NULL OR CAST(e.status AS string) = :status) " +
           "AND (:keyword IS NULL OR e.title LIKE %:keyword% OR e.description LIKE %:keyword%) " +
           "ORDER BY e.createdAt DESC")
    Page<Exam> searchExams(@Param("courseId") Long courseId,
                          @Param("examType") String examType,
                          @Param("status") String status,
                          @Param("keyword") String keyword,
                          Pageable pageable);
    
    /**
     * 发布所有草稿状态的考试
     */
    @Modifying
    @Query("UPDATE Exam e SET e.status = 'PUBLISHED' WHERE e.status = 'DRAFT'")
    void updateStatusFromDraftToPublished();
}